<?php

/**
 * Modul pro lokalni aktualizaci.
 *
 * @author David Dusek
 */
use CMS\Module\Exceptions;

class UpdateModule extends CMS\AbstractModule {
    public function hookAdminMenu() {
        return array("title" => "adminMenuTitle", 
                     "link" => ":Update:Update:default",                     
                     "img" => "btn_update.png");
    }
    
    public function hookAdminSubmenu() {
        $submenu = array(
            0 => array(
                "title" => "update",
                "link" => ":Update:Update:default",
                "icon" => "ui-icon-refresh",
                "right" => "update"
            ),
            1 => array (
                "title" => "settings",
                "link" => ":Update:Update:settings",
                "icon" => "ui-icon-wrench",
                "right" => "settings"
            ),
        );
        return $submenu;
    }
    
    public function hookUserGetAcl() {
        $acl = array(
            "Update" => array(
                "name" => "update_aclTitle",
                "operations" => array(
                    "settings" => "update_aclSettings", 
                    "update" => "update_aclUpdate"                  
                ),
            ),
        );
        
        return $acl;
    }
    
    /**
     * Pomocna funkce pro rekurzivni presunuti slozky a jejiho obsahu. Puvodni
     * obsah zdrojove slozky je smazan.
     * Funkce prevzata z diskuze na strance: http://php.net/manual/en/function.copy.php.
     * @param string $src Zdrojova slozka. Tato slozka bude po provedeni operace
     * prazdna.
     * @param string $dst Cilova slozka
     */
    public static function recurseMove($src, $dst) {
        $dir = opendir($src);
        @mkdir($dst);
        while (false !== ($file = readdir($dir))) {
            if (( $file != '.' ) && ( $file != '..' )) {
                if (is_dir($src . '/' . $file)) {
                    self::recurseMove($src . '/' . $file, $dst . '/' . $file);
                    rmdir($src . '/' . $file);
                } else {
                    copy($src . '/' . $file, $dst . '/' . $file);
                    unlink($src . '/' . $file);
                }
            }
        }
        closedir($dir);
    }
    
    /**
     * Pomocna funkce pro rekuryivni kopirovani jedne ze zdrojove slozky dane
     * parametrem $src d ocilove slozky zadane $dst.
     * Funkce prevzata z diskuze na strance: http://php.net/manual/en/function.copy.php.
     * @param string $src Zdrojova slozka.
     * @param string $dst Cilova slozka.
     */
    public static function recurseCopy($src, $dst) {
        $dir = opendir($src);
        @mkdir($dst);
        while (false !== ($file = readdir($dir))) {
            if (( $file != '.' ) && ( $file != '..' )) {
                if (is_dir($src . '/' . $file)) {
                    self::recurseCopy($src . '/' . $file, $dst . '/' . $file);
                } else {
                    copy($src . '/' . $file, $dst . '/' . $file);
                }
            }
        }
        closedir($dir);
    }
    
    /**
     * Pomocna funkce pro rekurzivni smazani slozky a jejiho obsahu.
     * @param type $path Cesta k mazane slozce.
     */
    public static function recurseDelete($path) {
        $dir = opendir($path);
        while (false !== ($file = readdir($dir))) {
            if (( $file != '.' ) && ( $file != '..' )) {
                if (is_dir($path . '/' . $file)) {
                    self::recurseDelete($path . '/' . $file);
                    rmdir($path . '/' . $file);
                } else {
                    unlink($path . '/' . $file);
                }
            }
        }
        closedir($dir);
    }
    
    /**
     * Fukce vytvori zalohu databaze (dump) a ulozi ji do slozky urcene 
     * parametrem destDir do souboru dump.sql.
     * @param \Doctrine\ORM\EntityManager $em Entity manager.
     * @param string $destDir Slozka pro ulozeni zalohy databaze.
     */
    public static function backupDatabase($em, $destDir) {
        $dumpClass = new \CMS\Update\Libs\DumpDatabase($em->getConnection());        
        $dumpClass->dump();
        
        if (!file_exists($destDir)) {
            @mkdir($destDir);
        }
        
        $destFile = $destDir . "dump.sql";
        @file_put_contents($destFile, $dumpClass->output);
    }
    
    /**
     * Dekodovani pozadavku RPC.
     * @param string $string Zasifrovane parametry pozadavku.
     * @param string $key Sdileny klic.
     * @return array 
     */
    public static function decode($string, $key) {
        $decoded = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($string), MCRYPT_MODE_CBC, md5(md5($key))), "\0");
        $params = explode(',', $decoded);
        return $params;
    }
}
